#include "stdafx.h"

static void create_guid(_pid_t pid, int argc, TCHAR* argv[])
{
    string basic_class_iid;
    basic_class_iid.clear();
    basic_class_iid.append("//");
    char szTemp[4096] = {0x00};
#if (TARGET_OS == OS_WIN)
    CharArrayPtr pBuf = wchar_to_char(argv[2], s_wstrlen(argv[2]));
    StringToGUIDClassName(pBuf, szTemp);
    basic_class_iid.append(pBuf);
#elif (TARGET_OS == OS_POSIX)
    StringToGUIDClassName(argv[2], szTemp);
    basic_class_iid.append(argv[2]);
#elif (TARGET_OS == OS_MACH)
    StringToGUIDClassName(argv[2], szTemp);
    basic_class_iid.append(argv[2]);
#endif

    // {7DADD097-97B4-45ec-A04C-135604FB6934}
    //_DEF_CLSID(CLSID_CObjectRun,
    //    0x7dadd097, 0x97b4, 0x45ec, 0xa0, 0x4c, 0x13, 0x56, 0x4, 0xfb, 0x69, 0x34);
    basic_class_iid.append("\n");
    basic_class_iid.append("_DEF_CLSID( <GUID_NAME>,");
    basic_class_iid.append("\n");
    basic_class_iid.append("    ");
    basic_class_iid.append(szTemp);
    basic_class_iid.append(")");
    basic_class_iid.append("\n");
    debug_view(basic_class_iid.c_str());
}

static void create_plugin(_pid_t pid, int argc, TCHAR* argv[])
{
}

int main_t(_pid_t pid, int argc, TCHAR* argv[])
{
    string guidop;
    guidop.clear();
    char szTemp[4096] = {0x00};
#if (TARGET_OS == OS_WIN)
    CharArrayPtr pBuf = wchar_to_char(argv[1], s_wstrlen(argv[1]));
    StringToGUIDClassName(pBuf, szTemp);
    guidop.append(pBuf);
#elif (TARGET_OS == OS_POSIX)
    guidop.append(argv[1]);
#elif (TARGET_OS == OS_MACH)
    guidop.append(argv[1]);
#endif

    // /CLSID {fd82a057-9b40-4875-9a61-33d19d379034}
    if (guidop.compare("/CLSID") == 0) {
        create_guid(pid, argc, argv);

    } else if (guidop.compare("/plugin") == 0) {
        create_plugin(pid, argc, argv);
    }

    return 1;
}

#if (TARGET_OS == OS_POSIX)

int main(int argc, char* argv[])
{
    _pid_t pid = get_processid();
    return main_t(pid, argc, argv);
}

#elif (TARGET_OS == OS_MACH)

int main(int argc, char* argv[])
{
    _pid_t pid = get_processid();
    return main_t(pid, argc, argv);
}
#elif (TARGET_OS == OS_UNIX)

int main(int argc, char* argv[])
{
    _pid_t pid = get_processid();
    return main_t(pid, argc, argv);
}

#elif (TARGET_OS == OS_WIN)

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    int iRet = 0;
    int nArgs = 0;
    LPWSTR* szArglist = NULL;
    _pid_t pid = get_processid();
    szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
    iRet = main_t(pid, nArgs, szArglist);
    LocalFree(szArglist);
    return 1;
}
#endif
